package com.rabincorp.tactile.action;

import java.util.Enumeration;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public abstract class BaseAction extends Action {

    protected static final Logger LOG = Logger.getLogger(BaseAction.class);
    
    /** The default struts forward success mapping. */
    protected static final String FORWARD_SUCCESS = "success";
    
    /** The default struts forward error mapping. */
    protected static final String FORWARD_ERROR = "error";

    
    /**
     * {@inheritDoc}
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws
            Exception {

        if (LOG.isDebugEnabled()) {
            logHeaders(request);
        }

        final String forwardPath = executeUseAllParam(mapping, form, request,
                response);
        return (forwardPath != null ? mapping.findForward(forwardPath) : null);
    }

    /**
     * User that needs to use ActionMapping and ActionForm must override this method.<br />
     * The default behaviour is to delegate to @{@link BaseAction#execute(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return The forward string (see also the struts config) to which control
     *	should be transferred, or null if the response has been completed.
     * @throws Exception if the application business logic throws an exception.
     */
    protected String executeUseAllParam(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws
            Exception {
        return execute(form, request, response);
    }

    /**
     * Process the specified non-HTTP request, and create the corresponding non-HTTP
     * response (or forward to another web component that will create it), with provision
     * for handling exceptions thrown by the business logic. Return an action
     * forward string, describing where and how control should be forwarded, or
     * null if the response has already been completed.
     *
     * @param request The non-HTTP request we are processing.
     * @param response The non-HTTP response we are creating
     * @return The forward string (see also the struts config) to which control
     *	should be transferred, or null if the response has been completed.
     * @throws Exception if the application business logic throws an exception.
     */
    protected abstract String execute(ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception;

    @SuppressWarnings("rawtypes")
	protected void logHeaders(HttpServletRequest request) {
        Enumeration headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            String headerName = (String) headers.nextElement();
            if (LOG.isDebugEnabled()) {
                LOG.debug(headerName + "=" + request.getHeader(headerName));
            }
        }
    }
}